package com.iap.common.translator
{
	import com.iap.app.translator.Translator;
	import com.iap.common.model.Client;
	import com.iap.common.model.Instance;
	
	import flash.utils.getQualifiedClassName;

	public class ClientTranslator extends Translator
	{
		public static function translate(item:Object):Object
		{
			if(getQualifiedClassName(item) == "Array")
			{
				return translateFromArray(item as Array);
			} else {
				return translateItem(item);
			}
		}
		
		public static function translateFromArray(data:Array):Array
		{
			var list:Array = new Array();
			for each(var item:Object in data)
			{
				list.push(translateItem(item));
			}
			return list;
		}
		
		private static function translateItem(item:Object):Client
		{
			var result:Client = translateFromObject(item, getQualifiedClassName(Client)) as Client;
			if(item.Instances != null)
			{
				 var list:Array = [];
				for each(var ciObject:Object in item.Instances)
				{
					list.push(InstanceTranslator.translate(ciObject.Instance) as Instance);
				}
				result.instances = list;
			}
			return result;
		}
	}
}